package com.badibadi.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.activity.AddApplyFormActivity;
import com.badibadi.activity.ClubHomepageActivity;
import com.badibadi.infos.ClubListModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class XListView1Adapter_1 extends BaseAdapter {
	private Context context;
	private List<ClubListModel> list;
	private DisplayImageOptions options;
	private Message mes;
	private Bundle bun;
	private Results resultsdd;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(context);
					Utils.showMessage(context, context.getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:
				try {
					Utils.ExitPrgress(context);
					Utils.showMessage(context, msg.getData().getString("结果"));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 3:
				try {
					Utils.ExitPrgress(context);
					Utils.showMessage(context, context.getResources()
							.getString(R.string.l_xb113));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 4:
				try {
					Utils.ExitPrgress(context);
					Utils.showMessage(context, context.getResources()
							.getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 5:
				try {
					Utils.ExitPrgress(context);
					Utils.showMessage(context, context.getResources()
							.getString(R.string.l_xb55));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;

			default:
				break;
			}
		}
	};

	public XListView1Adapter_1(List<ClubListModel> clubListModels,
			Context context) {
		this.context = context;
		list = clubListModels;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		//    ("查看大小" + list.size());
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		convertView = inflater.inflate(
				R.layout.fragment_indexclub3_import_layout, null);
		ImageView club_list_item_img = (ImageView) convertView
				.findViewById(R.id.club_list_item_img);// 俱乐部图片
		TextView club_list_item_name = (TextView) convertView
				.findViewById(R.id.club_list_item_name);// 名字
		TextView club_list_item_activitynum = (TextView) convertView
				.findViewById(R.id.club_list_item_activitynum);//
		Button club_list_item_add = (Button) convertView
				.findViewById(R.id.club_list_item_add);
		club_list_item_name.setText(list.get(position).getName());
		// String text =
		// String.format(clubListModels.get(position).getName(),
		// 0);
		// SpannableStringBuilder style = new SpannableStringBuilder(text);
		// // 设置指定位置文字的颜色
		// style.setSpan(new ForegroundColorSpan(Color.BLUE), 10,
		// clubListModels.get(position).getName().length() + 10,
		// Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		club_list_item_name.setText(list.get(position).getName());
		club_list_item_activitynum.setText(list.get(position).getActivity());
		if (list.get(position).getAdd().equals("1")) {
			club_list_item_add.setText(R.string.Already_add);
			club_list_item_add.setTextColor(R.color.gray);
		} else if (list.get(position).getAdd().equals("0")) {
			club_list_item_add.setText(R.string.Add111);
			club_list_item_add.setTextColor(0xff3eb251);
			club_list_item_add.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// if (list.get(position).getIs_person() != null) {
					try {
						if (list.get(position).getIs_person().equals("0")) {// 判断是否有人数限制
							if (Integer.valueOf(list.get(position).getPrice()) <= Integer
									.valueOf(list.get(position).getGold())) {
								if (list.get(position).getIs_apply()
										.equals("0")) {// 判断是否需要申请表
									Add_club(list.get(position).getId(), "0",
											"");
								} else { // 不需要
									Intent intent = new Intent(context,
											AddApplyFormActivity.class);
									intent.putExtra("pan", "set");
									intent.putExtra("cid", list.get(position)
											.getId());
									intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									context.startActivity(intent);
								}
							} else {
								Utils.showMessage(
										context,
										context.getResources().getString(
												R.string.wx_txt_111));
							}
						} else if (list.get(position).getIs_person()
								.equals("1")) {// 有人数限制
							if (Integer.valueOf(list.get(position).getAddnum()) >= Integer
									.valueOf(list.get(position).getLimitnum())) {// 已达到人数限制
								Utils.showMessage(
										context,
										context.getResources().getString(
												R.string.wx_txt_110));
							} else {// 没有达到人数限制
								if (Integer.valueOf(list.get(position)
										.getPrice()) <= Integer.valueOf(list
										.get(position).getGold())) {
									if (list.get(position).getIs_apply()
											.equals("0")) {
										Add_club(list.get(position).getId(),
												"0", "");
									} else {
										Intent intent = new Intent(context,
												AddApplyFormActivity.class);
										intent.putExtra("pan", "set");
										intent.putExtra("cid",
												list.get(position).getId());
										intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
										context.startActivity(intent);
									}
								} else {
									Utils.showMessage(
											context,
											context.getResources().getString(
													R.string.wx_txt_111));
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				// }
			});
		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, ClubHomepageActivity.class);
				intent.putExtra("cid", list.get(position).getId());
				intent.putExtra("c_uid", list.get(position).getC_uid());
				intent.putExtra("is_join", list.get(position).getAdd());
				intent.putExtra("logo", list.get(position).getLogo());
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
		ImageLoader.getInstance().displayImage(
				Constants.BadiDownImgUrl + list.get(position).getLogo()
						+ Constants.appPhoto4img, club_list_item_img, options);
		return convertView;
	}

	public List<ClubListModel> getList() {
		return list;
	}

	public void setList(List<ClubListModel> list) {
		this.list = list;
	}

	private void Add_club(final String cid, final String is_apply,
			final String content) {
		Utils.showPrgress(context);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(context));
				map.put("cid", cid);
				map.put("is_apply", is_apply);
				if (is_apply.equals("1")) {
					map.put("contents", content);
				}
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/attendClub");
				if (content != null) {
					resultsdd = Utils.checkResult_NNN(context, content);
//					   ("加入俱乐部返回的参数是:" + content + " "
//							+ resultsdd.getRetmsg());
					if (resultsdd != null
							&& !resultsdd.getRetmsg().equals("null")) {
						handler.sendEmptyMessage(5);
					} else {
						handler.sendEmptyMessage(4);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

}
